package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.LoginActivity;
import com.weiyi.mashangzhu.Roomer.Bean.HouseListBean;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeHavePageBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by win7 on 2017/5/12.
 */

public class RecommendAdapter2 extends BaseAdapter {
    List<ThemeHavePageBean.DataBean.HouselistBean> list;
    Context context;
    List<String> urllist = new ArrayList<>();
    String info;
    List<HouseListBean> houseListBeanList;

    public RecommendAdapter2(List<ThemeHavePageBean.DataBean.HouselistBean> list, Context context, String info) {
        this.list = list;
        this.context = context;
        this.info = info;
    }


    public RecommendAdapter2(Context context, String info, List<HouseListBean> houseListBeanList) {
        this.context = context;
        this.info = info;
        this.houseListBeanList = houseListBeanList;
    }
    @Override
    public int getCount() {
        if (info.equals("search")){
            return houseListBeanList.size();
        }else {
            return list.size();
        }

    }

    @Override
    public Object getItem(int position) {
        if (info.equals("search")){
            return houseListBeanList.get(position);
        }else {
            return list.get(position);
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_list2, null);
            viewHolder.price_num = (TextView) convertView.findViewById(R.id.price_num);
            viewHolder.grade = (TextView) convertView.findViewById(R.id.collect_grade);
            viewHolder.collect = (ImageButton) convertView.findViewById(R.id.collect_mark);
            viewHolder.collect_img = (ImageView) convertView.findViewById(R.id.collect_img);
            viewHolder.collect_title = (TextView) convertView.findViewById(R.id.collect_title);
            viewHolder.collect_grade = (TextView) convertView.findViewById(R.id.collect_grade);
            viewHolder.collect_type = (TextView) convertView.findViewById(R.id.collect_type);
            viewHolder.collect_address = (TextView) convertView.findViewById(R.id.collect_address);
            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();
        if (info.equals("search")){
            urllist= MyApp.getUrlList2(houseListBeanList,position);
            viewHolder.price_num.setText("¥"+houseListBeanList.get(position).getHPrice() + "");
            viewHolder.grade.setText(houseListBeanList.get(position).getScoreave() + "");
            if (houseListBeanList.get(position).getFavstatus().equals("0")) {
                viewHolder.collect.setImageResource(R.mipmap.collect);
            } else {
                viewHolder.collect.setImageResource(R.mipmap.collect1);
            }
            final ViewHolder finalViewHolder1 = viewHolder;
            viewHolder.collect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SPUtils.get(context, "isLogin", "0").equals("0")) {
                        UiUtil.startActivity(context, LoginActivity.class);
                    } else if (SPUtils.get(context, "isLogin", "0").equals("1")) {
                        //已登录 进行收藏操作
                        OkHttpUtils.get()
                                .addParams("token",SPUtils.get(context,"token","")+"")
                                .addParams("id",houseListBeanList.get(position).getId()+"")
                                .url(API.BaseApi_R+API.CollectHouse_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(context,"数据加载失败，请稍后再试");

                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject=new JSONObject(response);
                                            if (jsonObject.getInt("status")==0){
                                                if (jsonObject.getString("msg").equals("收藏成功")){
                                                    finalViewHolder1.collect.setImageResource(R.mipmap.collect1);
                                                    houseListBeanList.get(position).setFavstatus("1");
                                                    notifyDataSetChanged();
                                                    T.showShort(context,"收藏成功");
                                                }else {
                                                    finalViewHolder1.collect.setImageResource(R.mipmap.collect);
                                                    houseListBeanList.get(position).setFavstatus("0");
                                                    notifyDataSetChanged();
                                                    T.showShort(context,"取消收藏成功");
                                                }
                                            }else {
                                                T.showShort(context,jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        Log.e("rrrrrrrrrrr","=="+response);
                                    }
                                });
                    }
                }
            });
            Glide.with(context).load(API.BaseApi_url + urllist.get(0)).error(R.mipmap.errer).into(viewHolder.collect_img);
            viewHolder.collect_title.setText(houseListBeanList.get(position).getHTitle() + "");
            viewHolder.grade.setText(houseListBeanList.get(position).getScoreave() + "");
            viewHolder.collect_type.setText(houseListBeanList.get(position).getHRoom() + "居 | ");
            viewHolder.collect_address.setText(houseListBeanList.get(position).getHDistrict() + "");
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,HouseDetailsActivity.class);
                    intent.putExtra("city",houseListBeanList.get(position).getHCity()+"");
                    intent.putExtra("id",houseListBeanList.get(position).getId()+"");
                    context.startActivity(intent);
                }
            });
        }else {
            urllist= MyApp.getUrlList(list,position);
            viewHolder.price_num.setText("¥"+list.get(position).getHPrice() + "");
            viewHolder.grade.setText(list.get(position).getScoreave() + "");
            String s="";
            if (list.get(position).getIsCollect().equals("0")) {
                viewHolder.collect.setImageResource(R.mipmap.collect);
                s="0";
            } else {
                viewHolder.collect.setImageResource(R.mipmap.collect1);
                s="1";
            }
            final String finalS = s;
            final ViewHolder finalViewHolder = viewHolder;
            viewHolder.collect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SPUtils.get(context, "isLogin", "0").equals("0")) {
                        UiUtil.startActivity(context, LoginActivity.class);
                    } else if (SPUtils.get(context, "isLogin", "0").equals("1")) {
                        //已登录 进行收藏操作
                        OkHttpUtils.get()
                                .addParams("token",SPUtils.get(context,"token","")+"")
                                .addParams("id",list.get(position).getId()+"")
//                                .addParams("favstatus", finalS)
                                .url(API.BaseApi_R+API.CollectHouse_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(context,"网络连接失败");

                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject=new JSONObject(response);
                                            if (jsonObject.getInt("status")==0){
                                                if (jsonObject.getString("msg").equals("收藏成功")){
                                                    finalViewHolder.collect.setImageResource(R.mipmap.collect1);
                                                    list.get(position).setIsCollect("1");
                                                    notifyDataSetChanged();
                                                    T.showShort(context,"收藏成功");
                                                }else {
                                                    finalViewHolder.collect.setImageResource(R.mipmap.collect);
                                                    list.get(position).setIsCollect("0");
                                                    notifyDataSetChanged();
                                                    T.showShort(context,"取消收藏成功");
                                                }
                                            }else {
                                                T.showShort(context,jsonObject.getString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        Log.e("rrrrrrrrrrr","=="+response);
                                    }
                                });

                    }
                }
            });
            Glide.with(context).load(API.BaseApi_url + urllist.get(0)).error(R.mipmap.errer).into(viewHolder.collect_img);
            viewHolder.collect_title.setText(list.get(position).getHTitle() + "");
            viewHolder.grade.setText(list.get(position).getScoreave() + "");
            viewHolder.collect_type.setText(list.get(position).getHRoom() + "居 | ");
            viewHolder.collect_address.setText(list.get(position).getHDistrict() + "");
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,HouseDetailsActivity.class);
                    intent.putExtra("id",list.get(position).getId()+"");
                    intent.putExtra("city",list.get(position).getHCity()+"");
                    context.startActivity(intent);
                }
            });
        }



        return convertView;
    }

    class ViewHolder {
        TextView grade, price_num, collect_title, collect_grade, collect_type, collect_address;
        ImageButton collect;
        ImageView collect_img;
    }
}
